<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\CompanyConfession as CompanyConfessionModel;
use app\common\model\Practice as PracticeModel;

/**
 * 寺院忏悔控制器
 * Class Companyconfession
 * @package app\home\controller
 */
class Companyconfession extends Base
{
    /**
     * [record  忏悔记录]
     * @param
     * @author [C。]
     * @return json
     */
    public function index()
    {
        $param = input('param.');
        $model = new CompanyConfessionModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        $map = [];
        try{
            if(empty($param['is_all'])){
                $map['c.mid'] = $user['id'];
            }else{
                $map['c.is_ni'] = ['in','2,3'];
            }
            $map['c.comid'] = $param['id'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size,'c.id,c.content,c.status,c.reply,c.need_kaishi,c.create_time,m.username,m.img,c.is_ni');
            foreach($lists as &$v){
                if($v['is_ni'] == 2 || $v['is_ni'] == 0){
                    $http = substr($v['img'],0,4);
                    if($http != 'http') {
                        $v['img'] = config('domain_name') . $v['img'];
                    }
                }else{
                    $v['img'] = 'https://fushan.oss-cn-hangzhou.aliyuncs.com/niming.jpg';
                    $v['username'] = '匿名';
                }
            }
        }catch (\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [record  忏悔记录]
     * @param
     * @author [C。]
     * @return json
     */
    public function record()
    {
        $param = input('param.');
        $model = new CompanyConfessionModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        $map = [];
        try{
            if(empty($param['home'])){
                $map['c.mid'] = $user['id'];
            }
            if(!empty($param['status'])){
                $map['c.status'] = $param['status'];
            }
            $map['c.comid'] = $param['id'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size,'c.id,c.content,c.status,c.reply,c.create_time,m.username,m.img,c.update_time');
            foreach($lists as &$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$v['img'];
                }
                $v['addtime'] = date('Y-m-d',strtotime($v['create_time']));
            }
        }catch (\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [add  添加]
     * @param
     * @author [C。]
     * @return json
     */
    public function add()
    {
        $param = input('param.');
        $model = new CompanyConfessionModel;
        $pmodel = new PracticeModel;
        $user = $this->user;
        try{
            $param['mid'] = $user['id'];
            $model->addData($param);
            $practice = $pmodel->getRound(['type'=>3],'content');
        }catch (\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '忏悔失败' . $e->getMessage());
        }
        return admin_json(1, '忏悔成功',$practice['content']);
    }
    /**
     * [kaishi  开示]
     * @param
     * @author [C。]
     * @return json
     */
    public function kaishi()
    {
        $param = input('param.');
        $model = new CompanyConfessionModel;
        $data = [];
        try{
            $data['id'] = $param['id'];
            $data['status'] = 1;
            $data['reply'] = $param['reply'];
            $model->editData($data);
        }catch (\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '失败' . $e->getMessage());
        }
        return admin_json(1, '成功');
    }
}